import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios'
import VueAxios from 'vue-axios'
import VueLazyLoad from 'vue-lazyload'
import VueCookie from 'vue-cookie'
import { Message, MessageBox } from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'

import env from './env'

const mock = true
if (mock) {
  require('./mock/api')
}

// 根据前端的跨域方式进行调整
axios.defaults.baseURL = '/api' // 用login.json时注释
// axios.defaults.baseURL = env.baseURL
axios.defaults.timeout = 8000 // 接口超时时间
// 接口拦截错误
axios.interceptors.response.use(
  function(response) {
    let res = response.data
    let path = location.pathname
    if (res.status == 0) {
      // 成功状
      return res.data
    } else if (res.status == 10) {
      // window.location.href = '/login'
      // return Promise.reject(res)
      if (path != '/index' || path != '/login') {
        // 未登录
        window.location.href = '/login'
      }
      return Promise.reject(res)
    } else {
      Message.warning(res.msg)
      return Promise.reject(res)
    }
  },
  error => {
    let res = error.response
    Message.error(res.data.message)
    return Promise.reject(error)
  }
)

Vue.use(VueAxios, axios)
Vue.use(VueLazyLoad, {
  loading: '/imgs/loading-svg/loading-bars.svg' // 图片加载动画
  // error: 'dist/error.png' // 图片未找到图片
})
Vue.use(VueCookie)
Vue.prototype.$message = Message
Vue.prototype.$msgbox = MessageBox
Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
